<?
	ini_set( "max_execution_time", 3000000 );
	
	include_once "../classes/init.php";
	$page = "";
	/* vas chercher tout les début de code postal de la page */
	$page = fopen("http://www.postalcodedownload.com/QUEBEC.html", "rb");
	if( !empty( $page ) ) {
		$contenu = stream_get_contents( $page );
		@$doc = DOMDocument::loadHTML( $contenu );
		$objXpath = new domxpath( $doc );
		$req = "/html/body/table/tr/td/font/table/tr/td/table/tr/td";
		$objNodeList = $objXpath->query( $req );
		foreach ( $objNodeList as $node ){
			$tabCP1[] = str_replace( "\t", "", str_replace( "\n", "", strip_tags( trim( $node->nodeValue ) ) ) );
		}
		//print_r( $tabCP1 );
	}
	/* vas chercher tout les code postaux*/
	$tabCP;
	if( count( $tabCP1 ) > 0 ) {
		foreach( $tabCP1 as $cp1 ) {
			$page = fopen("http://www.postalcodedownload.com/" . $cp1  . "/index.html", "rb");
			if( !empty( $page ) ) {
				$contenu = stream_get_contents( $page );
				@$doc = DOMDocument::loadHTML( $contenu );
				$objXpath = new domxpath( $doc );
				$req = "/html/body/table/tr/td/font/table/tr/td/table/tr/td";
				$objNodeList = $objXpath->query( $req );
				foreach ( $objNodeList as $node ){
					$code = str_replace( "\t", "", str_replace( "\n", "", strip_tags( trim( $node->nodeValue ) ) ) );
					if( !empty( $code ) ) {
						$req = "INSERT INTO codePostaux (codePostal) VALUES( '" . $code . "')";
						$objBD->query( $req );
					}
				}
			}
			if( count( $tabCP ) > 10 ) {
				print_r( $tabCP );
				exit;
			}
		}
	}


?>